package com.example.demo.controller;

import com.example.demo.entity.BaseResponse;
import com.example.demo.entity.MaterialEntity;
import com.example.demo.entity.MaterialSearchBean;
import com.example.demo.entity.ResultBody;
import com.example.demo.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MaterialController {

    @Autowired
    MaterialService materialService;

    @RequestMapping("/getMaterial")
    public MaterialEntity getMaterial(int id) {
        return new MaterialEntity();
    }

    @RequestMapping("/materialListTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<MaterialEntity> listMaterial() {
        List<MaterialEntity> taskList = materialService.materialFindAll();
        return new BaseResponse<MaterialEntity>(0, "success",
                taskList.size(), taskList);
    }

    @RequestMapping("/materialSaveTask")
    @ResponseBody
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody personSaveTask(MaterialEntity materialEntity){
        System.out.println(materialEntity.toString());
        if(materialService.materialSaveTask(materialEntity))
            return new ResultBody(0, "success");
        else
            return new ResultBody(1, "fail");
    }

    @RequestMapping("/materialDelTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody materialDelTask(int id){
        if(materialService.materialDelTask(id))
            return new ResultBody(0, "success");
        else
            return new ResultBody(1, "fail");
    }

    @RequestMapping("/materialSearchTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<MaterialEntity> materialSearchTask(MaterialSearchBean materialSearchBean){
        materialSearchBean.setStart((materialSearchBean.getPage()-1)*materialSearchBean.getLimit());
        List<MaterialEntity> materialEntityList = materialService.materialSearchTask(materialSearchBean);
        return new BaseResponse<MaterialEntity>(0, "success",
                materialService.materialSearchCount(materialSearchBean), materialEntityList);
    }
}
